package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.entity.*;
import net.zjitc.entity.VO.ApplyVo;
import net.zjitc.mapper.ApplyMapper;
import net.zjitc.mapper.CompanyMapper;
import net.zjitc.mapper.RecruitMapper;
import net.zjitc.mapper.YonghuMapper;
import net.zjitc.service.IApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @auther 郭梦婷
 * @create 2022-05-09 11:22:19
 * @describe 服务实现类
 */
@Service
@Transactional
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements IApplyService {
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private RecruitMapper recruitMapper;
    @Autowired
    private YonghuMapper yonghuMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Page<ApplyVo> getApply(String querystring, Integer pagenum, Integer pagesize) {
        Page<ApplyVo> page=new Page<>(pagenum,pagesize);
        if (querystring.length()==0){
            page = applyMapper.selectAllApply(page);
        }else {
            page = applyMapper.selectApply(page, querystring);

        }

        return page;
    }

    @Override
    public ApplyVo selectById(Integer id) {
        ApplyVo applyVo= applyMapper.getById(id);
        return applyVo;
    }

    @Override
    public int deleteById(int id) {
        int i = applyMapper.deleteById(id);
        return i;
    }

    @Override
    public int addApply(Integer recruitid, String username,String resume) {
        int insert=0;
        if (username.length()!=0&&recruitid!=null){
            QueryWrapper<Yonghu> wrapper=new QueryWrapper<>();
            wrapper.eq("username",username);
            Yonghu yonghu = yonghuMapper.selectOne(wrapper);

            QueryWrapper<Recruit> wrapper1=new QueryWrapper<>();
            wrapper1.eq("id",recruitid);
            Recruit recruit = recruitMapper.selectOne(wrapper1);

            Apply apply=new Apply();
            apply.setResume(resume);
            apply.setRecruitid(recruit.getId());
            apply.setYonghuid(yonghu.getId());
            apply.setComid(recruit.getComid());
            apply.setAddtime(new Date());
            insert = applyMapper.insert(apply);
        }

        return insert;
    }

    @Override
    public int IfApply(Integer recruitid, String username) {
        int flag=0;
        if (username.length()!=0&&recruitid!=null){
            QueryWrapper<Yonghu> wrapper=new QueryWrapper<>();
            wrapper.eq("username",username);
            Yonghu yonghu = yonghuMapper.selectOne(wrapper);

            QueryWrapper<Apply> wrapper1=new QueryWrapper<>();
            wrapper1.eq("recruitid",recruitid).eq("yonghuid",yonghu.getId());
            Apply apply = applyMapper.selectOne(wrapper1);
            if (apply!=null){
                flag=1;

            }

        }

        return flag;
    }

    @Override
    public Page<ApplyVo> getAllApplyByUsername(String username, Integer pagenum, Integer pagesize) {
        QueryWrapper<Yonghu> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        Yonghu yonghu = yonghuMapper.selectOne(wrapper);

        Page<ApplyVo> page=new Page<>(pagenum,pagesize);
        QueryWrapper<ApplyVo> wrapper1=new QueryWrapper<>();
        page= applyMapper.getAllApplyByUsername(page,yonghu.getId());
        return page;
    }

    @Override
    public Page<ApplyVo> selectJobSeek(int id, String querystring, Integer pagenum, Integer pagesize) {
        Page<ApplyVo> page=new Page<>(pagenum,pagesize);
        page=applyMapper.selectJobSeek(page,id,querystring);
        return page;
    }
}
